package sp.whereru;

import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * This class is used to draw an overlay of a route inside  onto a given google mapview,
 * based on a set of directions given as an ArrayList of GeoPoints
 * and a mode to determine if an indoor or an outdoor route.
 * 
 * @author Nick
 * @version 1
 **/
public class RouteOverlay extends Overlay {
		private ArrayList<GeoPoint> route;
		private int mode;

		/**
		 * A constructor for this overlay.
		 * @param route the ArrayList of GeoPoints for the route to be overlaid.
		 * @param mode an int representing which type of route, 0 for outdoor and 1 for indoor.
		 */
		public RouteOverlay(ArrayList<GeoPoint> route, int mode) {
			super();		
			this.route = route;
			this.mode = mode;
		}

		@Override
		public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
			super.draw(canvas, mv, shadow);
			drawPath(mv, canvas);
			return true;
		}
		
		/**
		 * This method draws a path on a given MapView.
		 * @param mv the MapView that the route will be drawn onto.
		 * @param canvas the canvas which contains the MapView.
		 */
		public void drawPath(MapView mv, Canvas canvas) {
			int xPrev = -1;
			int yPrev = -1; 
			int xNow = -1; 
			int yNow = -1;			
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			paint.setStrokeWidth(4);
			paint.setAlpha(100);
			
			if(mode == 0){
				if (route != null) {
					for (int i = 0; i < route.size() - 4; i++) {
						GeoPoint gp = route.get(i);
						Point point = new Point();
						mv.getProjection().toPixels(gp, point);
						xNow = point.x;
						yNow = point.y;
						if (xPrev != -1)
							canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);
						xPrev = xNow;
						yPrev = yNow;
					}
				}
			}
			else if(mode == 1){
				if (route != null) {
					for (int i = 0; i < route.size() - 1; i++) {
						GeoPoint gp = route.get(i);
						Point point = new Point();
						mv.getProjection().toPixels(gp, point);
						xNow = point.x;
						yNow = point.y;
						if (xPrev != -1)
							canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);
						xPrev = xNow;
						yPrev = yNow;
					}
				}
			}
			
		}
	}